package com.yer.note.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.yer.note.R;
import com.yer.note.entity.Diary;
import com.yer.note.entity.User;
import com.yer.note.service.ItemTouchHelperAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class DiaryAdapter extends RecyclerView.Adapter<DiaryAdapter.myViewHodler> implements ItemTouchHelperAdapter {
    private Context context;
    private ArrayList<Diary> diaries;
    private String username;
    private OnItemClick mOnItemClickLitener;
    private String diaryuuid;

    //  定义接口写条目点击事件和长按事件
    public interface OnItemClick {
        //长按事件
        void onItemLongClick(String diaryUUid);
    }
    //外部访问的方法
    public void setOnItemClickLitener(OnItemClick mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public DiaryAdapter(Context context, ArrayList<Diary> diaries) {
        this.context = context;
        this.diaries = diaries;
    }

    public DiaryAdapter(Context context, ArrayList<Diary> diaries, String username) {
        this.context = context;
        this.diaries = diaries;
        this.username = username;
    }

    @NonNull
    @Override
    public myViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //item_layout是Reclyerview每个格子的布局
        //获取recylerview格子的样式
//        View itemview = View.inflate(context, R.layout.diary_item_2,null);
        View itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.diary_item,parent,false);
        //返回根据myViewHodler（）处理完的新View
        return new myViewHodler(itemview);
    }

    @Override
    public void onBindViewHolder(@NonNull myViewHodler holder, int position) {
        Diary data = diaries.get(position);
        this.diaryuuid = data.getdiaryuuid();
        holder.modifiedTime.setText(data.getTime());
        holder.weather.setText(data.getWeather());
        holder.text.setText(data.getContext());
        if(User.user.isPic()){
            holder.imageView.setImageURI(User.user.getUri());
        }
        //直接给itemView绑定一个长按的点击事件
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                System.out.println("onBindViewHolder:"+data.toString());
                mOnItemClickLitener.onItemLongClick(data.getdiaryuuid());
                return false;
            }
        });
        holder.location.setText(data.getLocation());
        holder.label.setText(data.getLabel());
        holder.userName.setText(username);
    }

    @Override
    public int getItemCount() {
        return diaries.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        //交换位置
        Collections.swap(diaries,fromPosition,toPosition);
        notifyItemMoved(fromPosition,toPosition);
    }

    @Override
    public void onItemDelete(int position) {
        //移除数据
        //列表内的diary的uuid要和数据源内的diary的uuid一样才会被删除
        List<Diary> list = Diary.listAll(Diary.class);
        Diary diary = null;
        String removedUseruuid = diaries.get(position).getUseruuid();
        String removeDiaryuuid = diaries.get(position).getdiaryuuid();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUseruuid().equals(removedUseruuid)){
                if(list.get(i).getdiaryuuid().equals(removeDiaryuuid)){
                    diary = list.get(i);
                    diary.delete();
                    break;
                }
            }
        }
        diaries.remove(position);
        notifyItemRemoved(position);
    }

    //用于根据布局文件生成View
    public class myViewHodler extends RecyclerView.ViewHolder{
        private TextView modifiedTime;
        private TextView weather;
        private TextView text;
        private TextView location;
        private TextView label;
        private TextView userName;
        private CircleImageView imageView;
        private OnItemClick holderOnItemClickLitener;
        public myViewHodler(View itemview) {
            super(itemview);
            userName = itemview.findViewById(R.id.item_username);
            location = itemview.findViewById(R.id.item_location);
            label = itemview.findViewById(R.id.item_label);
            modifiedTime = (TextView)itemview.findViewById(R.id.item_modifyDate);
            weather  = (TextView)itemview.findViewById(R.id.item_weather);
            text = (TextView)itemview.findViewById(R.id.item_content);
            imageView = itemview.findViewById(R.id.item_head);
        }
    }
}

